package de.java.shortys.myhome.fuelmonitor.view;

import static de.java.shortys.myhome.api.images.ImageConstants.ICON_CANCEL;
import static de.java.shortys.myhome.api.images.ImageConstants.ICON_SAVE;
import static java.awt.BorderLayout.NORTH;

import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JToolBar;

import de.java.shortys.myhome.api.validation.exception.AttributeFieldNotFoundException;
import de.java.shortys.myhome.api.validation.exception.AttributeInformationFieldNotFoundException;
import de.java.shortys.myhome.api.view.AbstractSwingSubModuleView;

public class SubModuleViewFillingEdit extends AbstractSwingSubModuleView implements ISubModuleViewFilling {

	private FuelFillingDetailsView fuelFillingView;

	private JToolBar toolbar;

	private JButton toolbarItemSave;

	private JButton toolbarItemCancel;

	public SubModuleViewFillingEdit() {
		initialize();
	}

	public void addCancelListener(ActionListener listener) {
		toolbarItemCancel.addActionListener(listener);
	}

	public void addSaveListener(ActionListener listener) {
		toolbarItemSave.addActionListener(listener);
	}

	@Override
	public void clear() {
		// Empty
	}

	private void createFuelFillingView() {
		fuelFillingView = new FuelFillingDetailsView();
		getContentArea().add(fuelFillingView.build());
	}

	private void createToolbar() {
		toolbar = new JToolBar();
		toolbar.setFloatable(false);
		toolbar.setBorder(null);
		toolbar.setFocusable(false);
		toolbar.setBorderPainted(false);
		getContentArea().add(NORTH, toolbar);

		toolbarItemSave = new JButton("Speichern", ICON_SAVE);
		toolbar.add(toolbarItemSave);

		toolbarItemCancel = new JButton("Abbrechen", ICON_CANCEL);
		toolbar.add(toolbarItemCancel);
	}

	public String getAmount() {
		return fuelFillingView.getTfAmount().getText();
	}

	public String getCharges() {
		return fuelFillingView.getTfCharges().getText();
	}

	public String getDateFilled() {
		return fuelFillingView.getTfDateFilled().getText();
	}

	public String getDistance() {
		return fuelFillingView.getTfDistance().getText();
	}

	public void setAmount(String amount) {
		fuelFillingView.getTfAmount().setText(amount);
	}

	public void setCharges(String charges) {
		fuelFillingView.getTfCharges().setText(charges);
	}

	public void setDateFilled(String dateFilled) {
		fuelFillingView.getTfDateFilled().setText(dateFilled);
	}

	public void setDistance(String distance) {
		fuelFillingView.getTfDistance().setText(distance);
	}

	public JButton getToolbarItemCancel() {
		return toolbarItemCancel;
	}

	public JButton getToolbarItemSave() {
		return toolbarItemSave;
	}

	private void initialize() {
		createToolbar();
		createFuelFillingView();

	}

	public void setValid(String attributeId, boolean valid) throws AttributeFieldNotFoundException,
			AttributeInformationFieldNotFoundException {
		fuelFillingView.setValid(attributeId, valid);
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

}
